package com.iwunu.gateway.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.iwunu.gateway.entity.ImageMongo;
import com.iwunu.gateway.service.ImageMongodbService;
import com.iwunu.iot.common.util.DateUtil;

@Component
public class ImageMongodbServiceImpl implements ImageMongodbService{
	 	@Resource
	 	private MongoTemplate mongoTemplate;
		public  ImageMongo saveSendImageData(String startTime,String endTime,byte[] imageData,byte[] imageDataFeature,String userId,Integer pitNum,String preUrl,String clientId,Integer role,String remark) {
      	  ImageMongo image = new ImageMongo();
      	  image.setEndTime(endTime);
      	  image.setStartTime(startTime);
      	  image.setImageData(imageData);
      	  image.setFeature(imageDataFeature);
      	  image.setFlDeviceId(clientId);
      	  image.setPitNum(pitNum);
      	  image.setImageUrl(preUrl);
      	  image.setClientId(clientId);
      	  image.setUserId(userId);
      	  image.setValid(true);
      	  image.setStatus(0);
      	  image.setRole(role);
      	  image.setRemark(remark);
      	  mongoTemplate.insert(image);
      	  return image;
		}
		@Override
		public void saveImage(ImageMongo image) {
	      	  ImageMongo imageNew = new ImageMongo();
	      	imageNew.setEndTime(image.getEndTime());
	      	imageNew.setStartTime(image.getStartTime());
	      	imageNew.setImageData(image.getImageData());
	      	imageNew.setFlDeviceId(image.getClientId());
	      	imageNew.setPitNum(image.getPitNum());
	      	imageNew.setImageUrl(image.getImageUrl());
	      	imageNew.setClientId(image.getClientId());
	      	imageNew.setUserId(image.getUserId());
	      	imageNew.setSucc(false);
	      	imageNew.setValid(true);
	      	imageNew.setStatus(0);
	      	imageNew.setRole(image.getRole());
	      	imageNew.setRemark(image.getRemark());
	      	mongoTemplate.insert(image);
		}
		public ImageMongo getImageByDeviceId(String clientId,Integer pitNum) {
			Query query = new Query(Criteria.where("clientId").is(clientId).and("pitNum").is(pitNum));
			ImageMongo imageMongo = mongoTemplate.findOne(query, ImageMongo.class);
	        return imageMongo;
		  }
		/**
		 * *获取锁的本人所有人脸数据
		 * @param userId
		 * @param clientId
		 * @param type
		 * @return
		 */
		public List<ImageMongo>  getUserSendImageData(Integer userId, String clientId) {
			Query query = new Query(Criteria.where("clientId").is(clientId).and("userId").is(userId));
			List<ImageMongo> ImageMongos = mongoTemplate.find(query, ImageMongo.class);
			return ImageMongos;
		}
		/**
		 * *获取锁的本人所有人脸数据
		 * @param userId
		 * @param clientId
		 * @param type
		 * @return
		 */
		public List<ImageMongo>  getSendImageDataByRole(Integer role, String clientId) {
			Query query = new Query(Criteria.where("clientId").is(clientId).and("role").is(role).and("endTime").gte(DateUtil.nowDateTime()));
			List<ImageMongo> ImageMongos = mongoTemplate.find(query, ImageMongo.class);
			return ImageMongos;
		}
		/**
		 * *获取锁的所有人脸数据
		 * @param userId
		 * @param clientId
		 * @param type
		 * @return
		 */
		public List<ImageMongo>  getSendImageData(String clientId) {
			Query query = new Query(Criteria.where("clientId").is(clientId));
			List<ImageMongo> ImageMongos = mongoTemplate.find(query, ImageMongo.class);
			return ImageMongos;
		}
		/**
		 * *获取锁的个人人脸数据
		 * @param userId
		 * @param clientId
		 * @param type
		 * @return
		 */
		public List<ImageMongo>  getSendImageDataByUser(Integer userId,String clientId) {
			Query query = new Query(Criteria.where("userId").is(userId).and("clientId").is(clientId).and("isSucc").is(true).and("endTime").gte(DateUtil.nowDateTime()));
			List<ImageMongo> ImageMongos = mongoTemplate.find(query, ImageMongo.class);
			return ImageMongos;
		}	
		
		
		@Override
		public void updateImage(String clientId,Integer pitNum) {
			Query query = new Query(Criteria.where("clientId").is(clientId).and("pitNum").is(pitNum));
			Update update = Update.update("isValid", false).set("status", 3);
			mongoTemplate.findAndModify(query, update, ImageMongo.class);
		}
		
		
		@Override
		public void updateImagePitNum(String clientId,Integer pitNum,Integer newPitNum) {
			Query query = new Query(Criteria.where("clientId").is(clientId).and("pitNum").is(pitNum));
			Update update = Update.update("pitNum", newPitNum).set("status", 0);
			mongoTemplate.findAndModify(query, update, ImageMongo.class);
		}	
		@Override
		public void removeImageByDeviceId(String clientId) {
			Query query = new Query(Criteria.where("clientId").is(clientId));
			 mongoTemplate.remove(query, ImageMongo.class);
		}

		@Override
		public void updateImage(String clientId, Integer pitNum, String endTime, String startTime) {
			Query query = new Query(Criteria.where("clientId").is(clientId).and("pitNum").is(pitNum));
			Update update = Update.update("pitNum", pitNum).set("status", 100).set("endTime", endTime).set("startTime", startTime);
			mongoTemplate.findAndModify(query, update, ImageMongo.class);
		}
		@Override
		public void removeImageByDeviceId(ImageMongo imageMongo) {
			Query query = new Query(Criteria.where("clientId").is(imageMongo.getClientId()).and("pitNum").is(imageMongo.getPitNum()));
			 mongoTemplate.remove(query, ImageMongo.class);
		}


}
